/* 
 * @(#)SendMail.java    Created on Apr 21, 2013
 * Copyright (c) 2013 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.leruan.aasimp.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail implements Serializable {

    private static final long serialVersionUID = 1L;
    public String host = null;
    public String from = null;
    public String to = null;
    public String fileAttachment = null;
    public String subject = null;
    public String text = null;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getFileAttachment() {
        return fileAttachment;
    }

    public void setFileAttachment(String fileAttachment) {
        this.fileAttachment = fileAttachment;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean sendM() {
        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.timeout", "25000");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mail.properties");
        Properties p = new Properties();
        try {
            p.load(inputStream);
            inputStream.close();
        }
        catch (IOException e1) {
            e1.printStackTrace();
        }
        final String account = p.getProperty("mail.account");
        final String password = p.getProperty("mail.password");
        Session sendMailSession = Session.getInstance(props, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(account, password);
            }
        });
        // 声名 message
        MimeMessage message = new MimeMessage(sendMailSession);
        try {
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setSentDate(new Date());
            // 建立 message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            // 内容；
            messageBodyPart.setText(text);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            // 附件；
            messageBodyPart = new MimeBodyPart();
            if (fileAttachment != null && !fileAttachment.equals("")) {
                DataSource source = new FileDataSource(fileAttachment);
                messageBodyPart.setDataHandler(new DataHandler(source));
                messageBodyPart.setFileName(fileAttachment);
                multipart.addBodyPart(messageBodyPart);
            }
            message.setContent(multipart);
            Transport.send(message);
            return true;
        }
        catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

    public int send(String to, String title, String text, String path) {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mail.properties");
        Properties p = new Properties();
        try {
            p.load(inputStream);
            inputStream.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String account = p.getProperty("mail.account");
        String host = p.getProperty("mail.host");
        SendMail sm = new SendMail();
        sm.setFileAttachment(path);
        sm.setFrom(account);
        sm.setTo(to);
        sm.setHost(host);
        sm.setSubject(title);
        sm.setText(text);
        int i = -1;
        if (sm.sendM()) {
            i = 1;
        }
        else {
            i = 2;
        }
        return i;
    }
}
